<template>
    <div>
        <h1>会员添加</h1>
        <table class="">
            <tr>
                <td>会员昵称：</td>
                <td>
                    <input type="text" v-model="data.mname">
                </td>
            </tr>
            <tr>
                <td>会员年龄：</td>
                <td>
                    <input type="text" v-model="data.age">
                </td>
            </tr>
            <tr>
                <td>手机号：</td>
                <td>
                    <input type="text" v-model="data.phone">
                </td>
            </tr>
            <tr>
                <td>累计消费次数：</td>
                <td>
                    <input type="text" v-model="data.price">
                </td>
            </tr>
            <tr>
                <td>会员等级：</td>
                <select v-model="data.did" >
                    <option value="0">请选择</option>
                    <option v-for="d in bdata" :value="d.lId">{{d.dname}}</option>
                </select>
            </tr>
            <tr>
                <td>会员头像：</td>
                <td>
                    <input type="file" @change="updale">
                    <img :src='"https://localhost:7237"+data.img' style="width: 60px; height:60px;">
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="button" value="添加" @click="add" class="btn btn-success">
                </td>
            </tr>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref,onMounted } from 'vue';
import axios from 'axios';
import { useRouter,useRoute } from 'vue-router';
import { useStore } from '@/stores/info';
const userst=useStore();

const router=useRouter();
const route=useRoute();


const updale=(e:any)=>{
    debugger
    var g=e.target.files[0]; 
    var fs=new FormData();
    fs.append("file",g)
    if(g.size>2*1024*1024){
        alert("图片上传大小不得超过2MB");
        return ;
    }
    debugger
    if(g.type!="image/jpeg" && g.type!="image/png"){
        alert("图片类型只能为jpg、png");
        return ;
    }
    axios.post("https://localhost:7237/api/Upsc/Upload",fs).then(res=>{
        data.value.img=res.data;
    })
}


const data=ref({
    
  "mid": 0,
  "mname": "",
  "age": 0,
  "phone": "",
  "price": "",
  "did": 0,
  "state": true,
  "img": ""

})

const add=()=>{
    if(data.value.mname=="张辉"){
    alert('相同的会员昵称不能出现多次！')
    return
   }
   if(data.value.mname=="张伟"){
    alert('相同的会员昵称不能出现多次！')
    return
   }
    axios.post("https://localhost:7237/api/Men/Add",data.value).then(res=>{
        if(res.data>0){
            alert('添加成功');
            router.push({path:"/menshow"})
        }
        else{
            alert('添加失败');
        }
    })
}
const bdata=ref([{
    "lId": 0,
    "dname": ""
}])
onMounted(()=>{
    if(!userst.usert.username){
        router.push({path:"/"})
    }
    bang()
})

const bang=()=>{
    axios.get("https://localhost:7237/api/Men/Dbang").then(res=>{
        bdata.value=res.data
    })
}
</script>

<style scoped>

</style>